package com.foosun.core.base;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * service基类.
 *
 * @param <T> 实体类类型
 */
@Transactional
public abstract class BaseService<T extends BaseEntity> {
    protected BaseDao<T> baseDao;

    public BaseDao<T> getBaseDao() {
        return baseDao;
    }

    public abstract void setBaseDao(BaseDao<T> baseDao);

    @Transactional(readOnly = true)
    public List<T> findAll() throws Exception {
        return baseDao.findAll();
    }

    @Transactional(readOnly = true)
    public Map findMax(String property) throws Exception {
        return baseDao.findMax(property);
    }

    @Transactional(readOnly = true)
    public T findMaxOneByGroup(String property, String gName, Object gValue) throws Exception {
        return baseDao.findMaxOneByGroup(property, gName, gValue);
    }


    @Transactional(readOnly = true)
    public T findMaxOne(String property) throws Exception {
        return baseDao.findMaxOne(property);
    }


    @Transactional(readOnly = true)
    public Map findMaxByGroup(String property, String gName, Object gValue) throws Exception {
        return baseDao.findMaxByGroup(property, gName, gValue);
    }

    @Transactional(readOnly = true)
    public PageData<T> findList(PageData<T> pageData) throws Exception {
        return baseDao.findList(pageData);
    }

    @Transactional
    public void delete(Integer id) throws Exception {
        baseDao.delete(id);
    }

    @Transactional
    public void delete(T entity) throws Exception {
        baseDao.delete(entity.getId());
    }

    @Transactional
    public void deleteAll() throws Exception {
        baseDao.deleteAll();
    }

    @Transactional(readOnly = true)
    public List<T> findAll(int topNum) throws Exception {
        return baseDao.findAll(topNum);
    }

    @Transactional(readOnly = true)
    public List<T> findByIds(List<Integer> ids) throws Exception {
        return baseDao.findByIds(ids);
    }

    @Transactional(readOnly = true)
    public List<T> findByProperty(String property, Object value) throws Exception {
        return baseDao.findByProperty(property, value);
    }

    @Transactional(readOnly = true)
    public List<T> findByProperty(String property, Object value, int topNum) throws Exception {
        return baseDao.findByProperty(property, value, topNum);
    }

    @Transactional(readOnly = true)
    public List<T> findByProperty(String[] property, Object[] value) throws Exception {
        return baseDao.findByProperty(property, value);
    }

    @Transactional(readOnly = true)
    public T findByPropertyOne(String property, Object value) throws Exception {
        return baseDao.findByPropertyOne(property, value);
    }

    @Transactional
    public void updateByID(Integer id, String property, Object value) throws Exception {
        baseDao.updateByID(id, property, value);
    }

    @Transactional
    public T findById(Integer id) throws Exception {
        return baseDao.findById(id);
    }

    @Transactional(readOnly = true)
    public List<T> findBySql(String sql, Object[] params) throws Exception {
        return baseDao.findBySql(sql, params);
    }

    @Transactional
    public void update(T entity) throws Exception {
        baseDao.update(entity);
    }

    @Transactional
    public List<T> findByExample(T entity) throws Exception {
        return baseDao.findByExample(entity);
    }

    @Transactional
    public int countByExample(T entity) throws Exception {
        return baseDao.countByExample(entity);
    }

    @Transactional
    public void save(List<T> list) throws Exception {
        for (T entity : list) {
            this.save(entity);
        }
    }

    @Transactional
    public void save(T entity) throws Exception {
        if (entity.isNew()) {
            baseDao.save(entity);
        } else {
            baseDao.update(entity);
        }
    }

}
